import os,flask
from multiprocessing import Process
from main.go import killApiOut


def after_request(response):
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
    response.headers['Access-Control-Allow-Headers'] = 'Content-Type,Authorization'
    return response

api = flask.Flask(__name__)
api.after_request(after_request)

@api.route('/jaina',methods=['post'])
def startTest():
    cmd = flask.request.values.get('cmd')
    # python解释器绝对路径(读取事先设置好的python家目录)
    JainaPython = os.getenv('JainaPython').replace('\\','\\\\')
    JainaPython = JainaPython+"python.exe"

    def startSSH(ip,port,user,passwd,size,timeout,apiport,outport):
        os.system(JainaPython + " ..\\main\\socket\\sshSendCmdAPI.py -l " + ip + " -P " + port + " -u " + user +  " -p " + passwd + " -s " + size + " -t " + timeout + " -i " + apiport + " -o " + outport)

    apiport = flask.request.values.get('apiport')
    outport = flask.request.values.get('outport')
    if cmd == 'start':
        ip = flask.request.values.get('ip')
        port = flask.request.values.get('port')
        user = flask.request.values.get('user')
        passwd = flask.request.values.get('passwd')
        size = flask.request.values.get('size')
        timeout = flask.request.values.get('timeout')
        Process(target=startSSH(ip,port,user,passwd,size,timeout,apiport,outport)).start()
    elif cmd == 'stop':
        killApiOut(apiport,outport)

    return "200"
if __name__ == '__main__':
    # api.run(port=args.apiport,debug=True,host='127.0.0.1')
    api.run(port=1111,debug=False,host='127.0.0.1')